package com.xsh.gls.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    public void setStr(String key, String value) {
        setStr(key, value, null);
    }


    public void setStr(String key, Object value, Long time) {
        if (value == null) {
            return;
        }
        if (value instanceof String) {
            String obj = (String) value;
            stringRedisTemplate.opsForValue().set(key, obj);
        } else if (value instanceof List) {
            List obj = (List) value;
            stringRedisTemplate.opsForList().leftPushAll(key, obj);
        } else if (value instanceof Map) {
            Map obj = (Map) value;
            stringRedisTemplate.opsForHash().putAll(key, obj);
        }
        if (time != null)
            stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
    }

    public Object getKey(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void delKey(String key) {
        stringRedisTemplate.delete(key);
    }

    public void set(String key,Object value){
        redisTemplate.opsForValue().set(key,value);
    }
    public Object get(String key){
       return redisTemplate.opsForValue().get(key);
    }
    public void del(String key){
        redisTemplate.delete(key);
    }

}

